<template>
  <div class="app-container">

    <el-button type="primary" style="margin: 10px" @click="handleAdd">添加顶级部门</el-button>
    <el-table
      :data="departmentList"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'parent'}">
      <el-table-column prop="name" label="部门名称" sortable width="180">
      </el-table-column>
      <el-table-column prop="leader" label="负责人" sortable width="180">
      </el-table-column>
      <el-table-column prop="path" label="地址">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="success" size="small" @click="handleAddDepartment(scope)">添加</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'Edit department':'New department'">
      <el-form :model="department" label-width="90px" label-position="left">
        <el-form-item label="名字" prop="name">
          <el-select v-model="department.leaderId" filterable clearable placeholder="输入负责人">
            <el-option
              v-for="user in userList"
              :key="user.id"
              :label="user.name"
              :value="user.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="department.name" placeholder="部门名称"/>
        </el-form-item>
        <el-form-item v-if="dialogType!=='top'" label="父部门" prop="parentId">
          <el-cascader
            v-model="department.parentId"
            placeholder="试试搜索：智能部"
            :options="departmentList"
            :props="SetCustomDept"
            clearable
            filterable>
          </el-cascader>
        </el-form-item>
        <el-form-item label="地址" prop="path">
          <el-input v-model="department.path" placeholder="地址"/>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmDepartment">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addDepartment, deleteDepartment, getDepartments, modifyDepartment } from '@/api/department'
import { getUserList } from '@/api/user'
import { deepClone } from '@/utils'

const defaultDepartment = {
  id: '',
}

export default {
  name: 'DepartmentManager',
  created() {
    this.getDepartment()
    this.getUserList()
  },
  data() {
    return {
      department: Object.assign({}, defaultDepartment),
      departmentList: [],
      userList: [],
      dialogVisible: false,
      dialogType: 'new',
      SetCustomDept: {
        value: 'id',
        label: 'name',
        children: 'children',
        checkStrictly: true
      },
    }
  },
  methods: {
    async getDepartment() {
      const res = await getDepartments(-1);
      this.departmentList = res.data.children
    },
    async getUserList() {
      const res = await getUserList()
      this.userList = res.data
    },
    handleAdd() {
      this.department = Object.assign({}, defaultDepartment)
      this.dialogType = 'top'
      this.dialogVisible = true
      this.department.parentId = 0
    },
    handleAddDepartment(scope) {
      this.department = Object.assign({}, defaultDepartment)
      this.dialogType = 'new'
      this.dialogVisible = true
      this.department.parentId = scope.row.id
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.checkStrictly = true
      this.department = deepClone(scope.row)
    },
    async handleDelete({ $index, row }) {
      this.$confirm('确定要删除吗?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteDepartment(row.id, 'FALSE')
          this.departmentList.splice($index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => {
          console.error(err)
        })
      await this.getDepartment()
    },
    async confirmDepartment() {
      if (this.dialogType === 'edit') {
        const val = this.department.parentId
        this.department.parentId = !isNaN(val) ? val : val.pop()
        await modifyDepartment(this.department.id, this.department)
      } else {
        const val = this.department.parentId
        this.department.parentId = !isNaN(val) ? val : val.pop()
        await addDepartment(this.department)
      }
      await this.getDepartment()
      this.dialogVisible = false
      this.$notify({
        title: 'Success',
        dangerouslyUseHTMLString: true,
        message: "操作成功",
        type: 'success'
      })
    }
  }
}
</script>

<style scoped>

</style>
